package com.ynet.middleground.mobile.approve.controller;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.base.Throwables;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.mobile.approve.commom.ResponseResult;
import com.ynet.middleground.mobile.approve.mq.producer.SourceProducerBackupPlan;
import com.ynet.middleground.mobile.approve.request.BackupChannelResultSyncReq;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * BackupPlanController 备用补偿方案
 *
 * @author suremotoo
 * @date 2023/04/10 17:44
 */
@RestController
@RequestMapping("/backup")
@Api(value = "移动审批备用补偿相关服务", tags = "移动审批备用补偿相关服务")
public class BackupPlanController {

    @Autowired
    private SourceProducerBackupPlan sourceProducerBackupPlan;

    @PostMapping("/blxdToJkztydsp")
    @ApiOperation(value = "模拟渠道给移动审批发送消息同步 MQ 通知", notes = "模拟渠道给移动审批发送消息同步 MQ 通知")
    public ResponseResult<Object> sendApproveStateMessages(@RequestBody @Valid BackupChannelResultSyncReq req) {
        ResponseResult<Object> result = new ResponseResult<>();
        try {
            Map<String, Object> params =
                JSON.parseObject(JSON.toJSONString(req), new TypeReference<Map<String, Object>>() {});
            sourceProducerBackupPlan.sendApproveStateMessages(params);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(),
                "备用补偿-模拟渠道给移动审批发送消息同步 MQ 通知, case: {}", Throwables.getStackTraceAsString(e));
        }
        return result;
    }

}
